using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SouthernLand.Common.BOL.NhaDat;

namespace SouthernLand.Common.DAL.NhaDat
{
    public class HuongNhaDAL : BaseDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new HuongNha instance filled with the DataReader's current record data 
        /// </summary>
        private HuongNha GetHuongNhaFromReader(IDataReader dr)
        {
            var obj = new HuongNha();
            obj.HuongNhaID = (dr["HuongNhaID"] is DBNull) ? Int32.MinValue : (Int32) dr["HuongNhaID"];
            obj.TenHuongNha = (dr["TenHuongNha"] is DBNull) ? String.Empty : (String) dr["TenHuongNha"];
            obj.TenHuongNha_en = (dr["TenHuongNha_en"] is DBNull) ? String.Empty : (String) dr["TenHuongNha_en"];
            return obj;
        }

        #endregion

        /// <summary>
        /// Returns a collection of HuongNha with the data read from the input DataReader
        /// </summary>
        private List<HuongNha> GetHuongNhaCollectionFromReader(IDataReader dr)
        {
            var obj = new List<HuongNha>();
            while (dr.Read())
                obj.Add(GetHuongNhaFromReader(dr));
            return obj;
        }

        public int Insert(HuongNha obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_HuongNha_Insert", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@HuongNhaID", SqlDbType.Int).Value = obj.HuongNhaID;
                myCommand.Parameters.Add("@TenHuongNha", SqlDbType.NVarChar).Value = obj.TenHuongNha;
                myCommand.Parameters.Add("@TenHuongNha_en", SqlDbType.NVarChar).Value = obj.TenHuongNha_en;

                return myCommand.ExecuteNonQuery();
            }
        }

        public bool Update(HuongNha obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_HuongNha_Update", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@HuongNhaID", SqlDbType.Int).Value = obj.HuongNhaID;
                myCommand.Parameters.Add("@TenHuongNha", SqlDbType.NVarChar).Value = obj.TenHuongNha;
                myCommand.Parameters.Add("@TenHuongNha_en", SqlDbType.NVarChar).Value = obj.TenHuongNha_en;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        public bool Delete(Int32 HuongNhaID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_HuongNha_Delete", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@HuongNhaID", SqlDbType.Int).Value = HuongNhaID;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        /// <summary>
        /// Retrieves the w4u_HuongNha with the specified ID
        /// </summary>
        public HuongNha GetHuongNhaByID(Int32 HuongNhaID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_HuongNha_GetHuongNhaByID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@HuongNhaID", SqlDbType.Int).Value = HuongNhaID;

                SqlDataReader dr = myCommand.ExecuteReader();
                return (dr.Read() ? GetHuongNhaFromReader(dr) : null);
            }
        }

        //Get All HuongNha
        public List<HuongNha> GetAllHuongNha(int PageIndex, int PageSize)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_HuongNha_GetAllHuongNha", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
                SqlDataReader dr = myCommand.ExecuteReader();
                return GetHuongNhaCollectionFromReader(dr);
            }
        }

        // Get Count All records of Table  HuongNha
        public int GetCountHuongNha()
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_HuongNha_GetCountHuongNha", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                return (int) myCommand.ExecuteScalar();
            }
        }
    }
}